package concurrency.chapter3;

import java.util.concurrent.TimeUnit;

/**
 * @author honway.liu
 * @date 2019-06-04 23:29
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class ThreadCloseDemo {

    static class DemoThread extends Thread {

        private volatile boolean flag = true;

        @Override
        public void run() {
            while (flag) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println("DemoThread is run.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void shutdown() {
            flag = false;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        DemoThread demoThread = new DemoThread();
        demoThread.start();
        TimeUnit.SECONDS.sleep(7);
        demoThread.shutdown();
    }
}
